# 购物系统业务需求V1.0：（需要使用到列表技术）
# 购物：
#         1.用户的账户里有钱
#         2.用户可推一个空的购物车进商场
#         3.开始购物，将自己想要的商品添加购物车
#         4.打印购物小条
#
# 程序和数据流程梳理分析：
# 	购前：
# 		钱要有：money
# 		有足够商品：列表来存储list
# 		有购物车：列表list
#
# 	购中：
# 		是否有这个商品：
# 			有
# 				钱够不够：
# 					够：
# 						添加到购物车！
# 						钱减去商品价格！
# 						恭喜，购买成功！您的卡余额还剩xxx
# 					不够：
# 						穷鬼，钱不够！请到其他超市购买！
# 			没有：
# 				没有这个商品，别瞎弄！
# 	购后：
# 		输入Q，q: 退出：
# 		打印购物小条！
#
# global money
# global goods
# global cart
# money = 500
# 货物
# goods = [
#     ['1', 20],
#     ['2', 50],
#     ['3', 100],
#     ['4', 200],
#     ['5', 300],
#     ['6', 500],
#     ['7', 1000]
# ]
# cart = []
# def select():
#     print('请输入您要购买的物品：')
#     name = input()
#     k = -1
#     i = 0
#     for i in range(0, 7):
#         if name == goods[i][0]:
#             print(goods[i][0])
#             k = i
#     if k < 0:
#         print('没有这个商品，别瞎弄')
#         return k
#     if k >= 0:
#         return k
#
# def buy(location):
#     global money
#     price = goods[location][1]
#     if money >= price:
#         cart.append([goods[location][0], goods[location][1]])
#         money -= price
#         print('恭喜，购买成功！您的卡余额还剩：', money)
#     elif location > 0:
#         print('穷鬼，钱不够！请到其它超市购买')
#     print('继续购物请输入C或c,退出购物请输入Q或q')
#     command = input()
#     if command == 'C' or command == 'c':
#         location = select()
#         buy(location)
#     if command == 'Q' or command == 'q':
#         cartl = len(cart)
#         sum = 0
#         for i in range(cartl):
#             print(cart[i])
#
# location = select()
# while location < 0:
#     location = select()
# buy(location)


# 购物系统业务需求V2.0：
# 1.	现在用户在进商场之前需要从10张优惠券中，随机抽取一张优惠券。
# 比如抽到一个空凋的8折优惠券，后面在购物的过程中，空凋就可以按照8折后的价格来购买。
# 2.	优化购物小条的人性化打印。
# 	购物时间，卡余额，购物数量，购买总商品的价值。
# 3.	添加退货功能。在购买途中，某个商品不要了，退回到购物架中！

import random
import datetime
global money
global goods
global cart
global coupons
global mycoupons
money = 500
# 货物
goods = [
    ['1', 20],
    ['2', 50],
    ['3', 100],
    ['4', 200],
    ['5', 300],
    ['6', 500],
    ['7', 1000]
]
cart = []
# 优惠券
coupons = [
    ['1', 0.9],
    ['1', 0.8],
    ['7', 0.8],
    ['7', 0.7],
    ['7', 0.6],
    ['7', 0.5],
    ['7', 0.4],
    ['6', 0.8],
    ['5', 0.7],
    ['5', 0.6],
]
mycoupons = []
def select():
    print('请输入您要购买的物品：')
    name = input()
    k = -1
    i = 0
    for i in range(0, 7):
        if name == goods[i][0]:
            print(goods[i][0])
            k = i
    if k < 0:
        print('没有这个商品，别瞎弄')
        return k
    if k >= 0:
        return k



def buy(location):
    global money
    if mycoupons[0] == goods[location][0]:
        price = goods[location][1]*mycoupons[1]
    else:
        price = goods[location][1]
    if money >= price:
        if mycoupons[0] == goods[location][0]:
            cart.append([goods[location][0], price, datetime.time])
        else:
            cart.append([goods[location][0], goods[location][1], datetime.datetime.today()])
        money -= price
        print('恭喜，购买成功！您的卡余额还剩：', money)
    else:
        print('穷鬼，钱不够！请到其它超市购买')
    act()


def act():
    global money
    print('继续购物请输入C或c,退货请输入R或r,退出购物请输入Q或q')
    command = input()
    if command == 'C' or command == 'c':
        location = select()
        buy(location)
    if command == 'R' or command == 'r':
        rname = input('请输入要退回的货物：')
        p = 0
        for i in range(len(cart)):
            if rname == cart[i][0]:
                money += cart[i][1]
                del cart[i]
                print('货物已退回，您的卡余额还剩：', money)
                p = 1
                act()
        if p == 0:
            print('未在您的购物车里发现您要退的货物')
            act()
    if command == 'Q' or command == 'q':
        cartl = len(cart)
        sum = 0
        for i in range(cartl):
            print(cart[i])
            sum += cart[i][1]
        print('购买商品数量为：', cartl, '   共花费', sum)
    else:
        print('错误命令')
        act()



mycoupons = random.choice(coupons)
print('恭喜您抽中优惠券：', mycoupons)
location = select()
while location < 0:
    location = select()
buy(location)
